package com.ia2014.tp.portal2.integration.mdb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

import com.ia2014.tp.portal2.facades.FacadeIntegracion; 
import com.ia2014.tp.portal2.log.Log;

//@ActivationConfigProperty(propertyName = "jndiParams", propertyValue = "java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory;java.naming.provider.url=http-remoting://127.0.0.1:8080;java.naming.security.principal=quickstartUser;java.naming.security.credentials=quickstartPwd1!")

/**
 * Message-Driven Bean implementation class for: ProcesarArticuloBean
 */

/*
 * Conexion a un Queue Remoto en otro Servidor
@MessageDriven(
		activationConfig = { 
				@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
				@ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/queue/deposito"),
				@ActivationConfigProperty(propertyName = "connectorClassName", propertyValue ="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory"),
				@ActivationConfigProperty(propertyName = "connectionParameters", propertyValue = "host=192.168.0.26;port=5445"),
				@ActivationConfigProperty(propertyName = "user", propertyValue = "deposito"),
				@ActivationConfigProperty(propertyName = "password", propertyValue = "deposito123.")
				},
		mappedName = "jms/queue/deposito")
*/
@MessageDriven(
		activationConfig = { 
				@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
				@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/deposito")
				},
		mappedName = "queue/deposito")
public class NuevoArticuloMDB implements MessageListener {
	
	
	private static Logger logger = Logger.getLogger(NuevoArticuloMDB.class);
	
	@EJB
	private FacadeIntegracion facadeIntegracion;

    public NuevoArticuloMDB() {
        // TODO Auto-generated constructor stub
    }
	
	/**
     * @see MessageListener#onMessage(Message)
     */
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
    	
    	String xml = null;
    	
    	if (message instanceof TextMessage) {
    		
    		
    		try {
    			xml = ((TextMessage) message).getText();
    		
    			logger.info("Recibiendo nuevo articulo.\nXml: " + xml);
    			
    			//System.out.println(xml);
    			
    			facadeIntegracion.agregarNuevoArticulo(xml);
							
			}  catch(Exception e){
				
				logger.error("Error en el procesamiento del mansaje Nuevo Articulo\n" + "xml:\n" + xml);
				logger.error(e.toString());
				logger.debug("Stacktrace", e);
				
			}
    	}
    }

}
